package com.facedemo.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.facedemo.MyApplication;
import com.facedemo.R;
import com.facedemo.db.entity.Person;
import com.facedemo.db.utils.PersonDaoUtils;
import com.facedemo.face.FaceDetectUtil;
import com.facedemo.face.callback.FaceCallback;
import com.facedemo.utils.SoundPoolUtils;
import com.facedemo.view.CameraSurface;
import com.facedemo.view.FaceMarkSurface;

//人脸识别
public class FaceDetectActivity extends Activity {
    private CameraSurface mCameraSurfaceView;
    private FaceMarkSurface surface_draw;
    private TextView tv_result;
    private Button btn_back;

    private FaceDetectUtil faceAttendanceUtil;
    private SoundPoolUtils soundPoolUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_face_detect);
        initView();

        soundPoolUtils = new SoundPoolUtils(FaceDetectActivity.this,new int[]{R.raw.success,R.raw.fail});

        faceAttendanceUtil = new FaceDetectUtil(MyApplication.faceConfig, mCameraSurfaceView,surface_draw);
        faceAttendanceUtil.setFaceCallback(faceCallback);
        faceAttendanceUtil.setStartFaceRun(true);
    }

    private void initView() {
        mCameraSurfaceView = findViewById(R.id.surface_camera);
        surface_draw = findViewById(R.id.surface_draw);

        tv_result = findViewById(R.id.tv_result);
        btn_back = findViewById(R.id.btn_back);

        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private String prePersonId = null;
    private FaceCallback faceCallback = new FaceCallback() {
        @Override
        public void detectionSuccess(String personId) {//人脸识别成功

            if(prePersonId==null || !prePersonId.equals(personId)) {
                prePersonId = personId;
                Person employee = PersonDaoUtils.queryById(personId);
                if(employee!=null && myHandler!=null) {
                    Message msg = new Message();
                    msg.what = 0x80;
                    msg.obj = employee.getName();
                    myHandler.sendMessage(msg);

                    myHandler.sendEmptyMessageDelayed(0x82,3000);
                }
            } else {
                if(myHandler!=null) {
                    myHandler.removeMessages(0x82);
                    myHandler.sendEmptyMessageDelayed(0x82,3000);
                }
            }
        }

        @Override
        public void detectionFailed(int filedNum) {
            if(filedNum==5) {
                if(myHandler!=null) myHandler.sendEmptyMessage(0x81);
            }
        }
    };

    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0x80:
                    tv_result.setText((String)msg.obj+" 验证通过!");
                    soundPoolUtils.playSound(R.raw.success);
                    break;
                case 0x81:
                    tv_result.setText("未注册人员/非活体");
                    soundPoolUtils.playSound(R.raw.fail);
                    break;
                case 0x82:
                    tv_result.setText("");
                    prePersonId = null;
                    break;
            }
        }
    };


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(faceAttendanceUtil!=null) {
            faceAttendanceUtil.destory();
            faceAttendanceUtil=null;
        }
        if(myHandler!=null) {
            myHandler.removeCallbacksAndMessages(null);
            myHandler = null;
        }
    }
}
